package club.dataresources.common;

import java.util.ArrayList;
import java.util.List;

/**
 * API 统一返回状态码
 *
 * @author zhumaer
 */
public enum ResultEnum {

    /* 2XX 请求成功 */
    SUCCESS(200, "request.success.200"),

    /* 3XX 重定向 */
    REDIRECT(300, "request.redirect.300"),

    /*4XX 请求错误*/
    /* 用户错误*/
    USER_NOT_PERMISSION(401, "request.error.user.not.permission.-401"),
    NO_ACTION(403, "request.error.no.action.-403"),

    /*5XX 服务器错误*/
    SEVER_ERROR(500, "sever.error.-500"),
    USER_ACCOUNT_ERROR(501, "user.account.error.501"),
    VERIFICATION_CODE_SEND_ERROR(502, "verification.code.send.error.502"),
    VERIFICATION_CODE_ERROR(503, "verification.code.error.503"),
    VERIFICATION_CODE_EXPIRED(504, "verification.code.expired.504"),
    USER_HAS_EXISTED(505, "user.has.existed.505"),
    USER_NOT_EXIST(506, "user.not.exist.506"),
    USER_LOGIN_ERROR(507, "user.login.error.507"),
    USER_ACCOUNT_CODE_ERROR(508, "user.account.code.error.508"),
    USER_HAS_LOGIN(509, "user.has.login.509"),
    USER_PASSWORD_CODE_ERROR(510, "user.password.code.error.510"),
    USER_FREQUENT_CODE_ERROR(511, "user.frequent.code.error.511"),
    USER_CONTENT_SENSITIVE(512, "user.content.sensitive.512"),
    USER_ID_ISEXIST(513, "user.id.isexist.513"),
    USER_CONTENT_ERR(514, "user.content.err.514");


    private Integer code;

    private String message;

    ResultEnum(Integer code, String message) {
        this.code = code;
        this.message = message;
    }


    public Integer getCode() {
		return code;
	}

	public String getMessage() {
		return message;
	}



	public static String getMessageByName(String name) {
        for (ResultEnum item : ResultEnum.values()) {
            if (item.name().equals(name)) {
                return item.message;
            }
        }
        return name;
    }

    public static Integer getCodeByName(String name) {
        for (ResultEnum item : ResultEnum.values()) {
            if (item.name().equals(name)) {
                return item.code;
            }
        }
        return null;
    }

    @Override
    public String toString() {
        return this.name();
    }

    /**
     * 校验重复的code值
     *
     * @param args
     */
    public static void main(String[] args) {
        ResultEnum[] apiResultCodes = ResultEnum.values();
        List<Integer> codeList = new ArrayList<Integer>();
        for (ResultEnum resultCode : apiResultCodes) {
            if (codeList.contains(resultCode.code)) {
                System.out.println(resultCode.code);
            } else {
                codeList.add(resultCode.getCode());
            }
        }
    }
}